package com.situ.feemanagement.service.impl;

import com.situ.feemanagement.mapper.*;
import com.situ.feemanagement.pojo.entity.Category;
import com.situ.feemanagement.pojo.entity.Medicine;
import com.situ.feemanagement.pojo.entity.Restocking;
import com.situ.feemanagement.pojo.vo.EChartsVO;
import com.situ.feemanagement.pojo.vo.IdVO;
import com.situ.feemanagement.pojo.vo.TotalVO;
import com.situ.feemanagement.service.IEchartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EchartsServiceImpl implements IEchartsService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private MedicineMapper medicineMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestockingMapper restockingMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<EChartsVO> selectEarning() {
        List<EChartsVO> list = new ArrayList<>();

        List<IdVO> medicineIdList = medicineMapper.selectIds();
        for (IdVO medicineId : medicineIdList) {
            Medicine medicine = medicineMapper.selectById(medicineId.getId());
            String name = medicine.getName();

            //计算药品的售出所得利润
            Integer number = orderMapper.selectByMedicineId(medicineId.getId());
            if (number == null) {
                number = 0;
            }
            double allSellPrice = number * medicine.getSellPrice();

            //计算药品的进货成本
            List<Restocking> restockingList = restockingMapper.selectByMedicineId(medicineId.getId());
            double allPurchasePrice = 0.0;
            if (restockingList != null) {
                Double purchasePrice = restockingList.get(restockingList.size()-1).getPurchasePrice();
                allPurchasePrice = number * purchasePrice;
            }

            //计算药品售卖所得净利润
            double value = allSellPrice - allPurchasePrice;
            EChartsVO earnings = new EChartsVO(name,value);
            list.add(earnings);
        }
        return list;
    }

    @Override
    public TotalVO selectTotalCount() {
        Integer userCount = userMapper.selectCount();
        Integer patientCount = patientMapper.selectCount();
        Integer medicineCount = medicineMapper.selectCount();
        Integer departmentCount = departmentMapper.selectCount();
        TotalVO totalVO = new TotalVO();
        totalVO.setUserCount(userCount);
        totalVO.setPatientCount(patientCount);
        totalVO.setMedicineCount(medicineCount);
        totalVO.setDepartmentCount(departmentCount);
        return totalVO;
    }

    @Override
    public Map<String, Object> selectCategoryCount() {
        List<Category> categoryList = categoryMapper.selectAll();
        Map<String, Object> map = new HashMap<>();
        List listName = new ArrayList();
        List listValue = new ArrayList();
        for (Category category : categoryList) {
            String name = category.getName();
            int CategoryId = category.getId();
            double value = medicineMapper.selectCountByCategoryId(CategoryId);
            listName.add(name);
            listValue.add(value);
        }
        map.put("name",listName);
        map.put("value",listValue);
        return map;
    }

    @Override
    public List<EChartsVO> selectInventoryCount() {
        List<EChartsVO> list = new ArrayList<>();
        List<Medicine> medicineList = medicineMapper.selectAll();
        for (Medicine medicine : medicineList) {
            String name = medicine.getName();
            double value = medicine.getInventory();
            EChartsVO inventoryCount = new EChartsVO(name,value);
            list.add(inventoryCount);
        }
        return list;
    }
}
